from ase.io import read, write
from ase.optimize import BFGS
from ase.neb import NEB
from ase.calculators.vasp import Vasp

initial = read('POSCAR.initial') 
final = read('POSCAR.final')  

# 设置VASP计算参数
vasp_params = {
    'prec': 'Accurate',           
    'xc': 'PBEsol', 
    'encut': 650,                 
    'ediff': 1e-6,                             
    'ismear': 0,
    'sigma':0.05,         
    'kspacing': 0.26,    
    'command': 'vasp_std'       
}

# 初始状态优化：
initial.calc = Vasp(**vasp_params)
qn = BFGS(initial, trajectory='initial.traj')
qn.run(fmax=0.05)

# 最终状态优化：
final.calc = Vasp(**vasp_params)
qn = BFGS(final, trajectory='final.traj')
qn.run(fmax=0.05)

# NEB计算
initial_opt = read('initial.traj')
final_opt = read('final.traj')

images = [initial_opt] + [initial_opt.copy() for i in range(3)] + [final_opt]

# 为每个图像设置计算器
for image in images:
    image.calc = Vasp(**vasp_params)

neb = NEB(images)
neb.interpolate()
qn = BFGS(neb, trajectory='neb.traj')
qn.run(fmax=0.05)
